<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <input type="button" onclick="deleteCheck()" class="btn btn-default" title="删除" value="删除" ><i class="fa fa-trash-o"></i>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">
			                          <tr><td><input  type="checkbox"></td>
				                          <td>1</td>
									      <td>1</td>
									      <td>促销海报1</td>
									      <td>http://wwww.hb.com/hd1.html</td>
									      <td><img alt="" src="" width="100px" height="50px"></td>
									      <td>1</td>
									      <td>有效</td>									     								     
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           
		                                  </td></tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->


<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">
			<form id="addForm" method="post" enctype="multipart/form-data">
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select  name="categoryId" class="form-control">
							<option th:each="category :${categoryList}" th:value="${category.id}" th:text="${category.name}"></option>
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  name="title"  class="form-control" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  name="url" class="form-control" placeholder="URL" >  </td>
		      	</tr>
		      	<tr>
		      		<td>排序</td>
		      		<td><input  name="sortOrder" class="form-control" placeholder="排序" >  </td>
		      	</tr>
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
								<input type="file" id="file"  name="image"/>
								<input class="btn btn-primary" onclick="uploadImage()" type="button" value="上传" >
					            </td>
								<td id="t">
									<input type="hidden"  name="url" value=" " />
								</td>
								<td id="td">
									<img  src="{{entity.pic}}" width="200px" height="100px">
								</td>
							</tr>
						</table>
		      		</td>
		      	</tr>
		      	<tr>
					<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" name="status" value="1" class="icheckbox_square-blue" >
		      		</td>
		      	</tr>
			 </table>
			</form>
		</div>
		<div class="modal-footer">
			<button class="btn btn-success" onclick="addContent()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">广告编辑</h3>
			  </div>
			  <div class="modal-body">
				  <form id="updateForm" method="post" enctype="multipart/form-data">
					  <table class="table table-bordered table-striped"  width="800px">
						  <input type="text" name="id" id="id" >
						  <tr>
							  <td>广告分类</td>
							  <td id="updateCategoryId">
								  <select id="categoryId" name="categoryId" class="form-control">
									  <option th:each="category :${categoryList}"  th:value="${category.id}" th:text="${category.name}"></option>
								  </select>
							  </td>
						  </tr>
						  <tr>
							  <td>标题</td>
							  <td><input id="title" name="title"  class="form-control" placeholder="标题" >  </td>
						  </tr>
						  <tr>
							  <td>URL</td>
							  <td><input id="url" name="url" class="form-control" placeholder="URL" >  </td>
						  </tr>
						  <tr>
							  <td>排序</td>
							  <td><input id="sortOrder" name="sortOrder" class="form-control" placeholder="排序" >  </td>
						  </tr>
						  <tr>
							  <td>广告图片</td>
							  <td>
								  <table id="updateImg">
									  <tr>
										  <td>
											  <input type="file" id="file1"  name="image"/>
											  <input class="btn btn-primary" onclick="uploadImage1()" type="button" value="上传" >
										  </td>
										  <td id="t1">
											  <input type="hidden"  name="url" value=" " />
										  </td>
										  <td id="td1">
											  <img  src="" width="200px" height="100px">
										  </td>
									  </tr>
								  </table>
							  </td>
						  </tr>
						  <tr>
							  <td>是否有效</td>
							  <td id="check">
								  <input type="checkbox" checked="" name="status" value="1" class="icheckbox_square-blue" >
							  </td>
						  </tr>
					  </table>
				  </form>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="updateContent()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

	<script>
		//首页展示
		$(function () {
			$.get("/contentController/getContentList",{},function (data) {
				console.log(data);
				if (data.code == 10000){
					var tbody = '';
					var data1 = data.data;
					for (let i = 0; i < data1.length; i++) {
						var tr ='<tr><td><input name="checkId"  value="'+data1[i].id+'" type="checkbox"></td>' +
								'<td>'+data1[i].id+'</td>' +
								'<td>'+data1[i].categoryId+'</td>' +
								'<td>'+data1[i].title+'</td>' +
								'<td>'+data1[i].url+'</td>' +
								'<td><img alt="" src="'+data1[i].pic+'" width="100px" height="50px"></td>' +
								'<td>'+data1[i].sortOrder+'</td>' +
								'<td>'+data1[i].status+'</td>' +
								'<td class="text-center">' +
								'<input type="button" value="修改" onclick="editContent('+data1[i].id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal1">' +
								'</td></tr>';
						tbody += tr;
					}
					$("#tbody").html(tbody);
				}else {
					alert(data.message)
				}
			})
		})


		//上传图片(增加)
		function uploadImage(){
			var form = document.getElementById("addForm");
			var file = new FormData(form);
			$.ajax({
				url: "file/upload",
				type: "POST",
				processData:false,
				contentType:false,
				//重要部分，data的传的是整个表单，不用大括号包裹；不用自定义变量名
				data:file,
				success: function (date) {
					if (date.code == 10000){
						$("#td").html('<img src="'+date.data+'" width="200px" height="100px">')
						$("#t").html('<input type="hidden"  name="pic" value="'+date.data+'" />')
					}
				},error: function (data) {
					alert(data.message);
				}
			})
		}
		//上传图片(修改)
		function uploadImage1(){
			var form = document.getElementById("updateForm");
			var file = new FormData(form);
			$.ajax({
				url: "file/upload",
				type: "POST",
				processData:false,
				contentType:false,
				//重要部分，data的传的是整个表单，不用大括号包裹；不用自定义变量名
				data:file,
				success: function (date) {
					if (date.code == 10000){
						$("#td1").html('<img src="'+date.data+'" width="200px" height="100px">')
						$("#t1").html('<input type="hidden"  name="pic" value="'+date.data+'" />')
					}
				},error: function (data) {
					alert(data.message);
				}
			})
		}

		//添加方法
		function addContent() {
			$.get("/contentController/addContent",$("#addForm").serialize(),function (data) {
				if (data.code == 10000){
					alert(data.message);
					window.location.reload();
				}else {
					alert(data.message)
				}
			})
		}

		//回显信息
		function editContent(id) {
			$.get("/contentController/editContent",{id:id},function (data) {
				$("#id").val(data.data.id)
				$("#title").val(data.data.title)
				$("#sortOrder").val(data.data.sortOrder)
				$("#url").val(data.data.url)
				$("#updateImg").html('<tr>' +
						'<td>' +
						'<input type="file" id="file1"  name="image"/>' +
						'<input class="btn btn-primary" onclick="uploadImage1()" type="button" value="上传" >' +
						'</td>' +
						'<td id="t1">' +
						'<input type="hidden"  name="url" value="'+data.data.pic+'" />' +
						'</td>' +
						'<td id="td1">' +
						'<img  src="'+data.data.pic+'" width="200px" height="100px">' +
						'</td>' +
						'</tr>')
			})
		}

		//修改方法
		function updateContent() {
			$.get("/contentController/updateContent",$("#updateForm").serialize(),function (data) {
				if (data.code == 10000){
					alert(data.message);
					window.location.reload();
				}else {
					alert(data.message)
				}
			})
		}

		//批量删除
		function deleteCheck() {
			//定义一个空数组一会来存放要删除的id
			let ids = [];
			let checkArr = document.getElementsByName("checkId");
			for(let i=0; i<checkArr.length; i++){
				if(checkArr[i].checked){
					ids.push(checkArr[i].value);
				}
			}
			if (ids.length > 0){
				let flag = confirm("您确定要删除吗?");
				if (flag){
					$.get("/contentController/deleteContent","ids="+ids,function (data) {
						if (data.code == 10000){
							alert("删除成功");
							window.location.reload();
						}else {
							alert("删除失败");
						}
					})
				}else {
					alert("删除失败");
				}
			}else {
				alert("请您选择要删除的行");
			}
		}
	</script>
</body>

</html>